﻿using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Table.DataServices;
using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Data.Services.Common;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using wlog.Model;
//http://www.windowsazure.com/en-us/develop/net/how-to-guides/table-services/#create-table
namespace wlog.Shell.Storage.Table
{
    public class TableStorageCommand : StorageCommand
    {
        public Type SearchTypeByName(string typeName)
        {
            Type foundType = null;
            Assembly hosted = Assembly.GetExecutingAssembly();
            string baseFullName = (typeof(TableServiceEntity)).FullName;
            Type[] tableTypes = (from a in hosted.GetTypes()
                                 where a.BaseType != null
                                 select a).ToArray();
            Results.WriteLine("================================================================================");

            foreach (var type in tableTypes)
            {
                if (string.Compare(type.BaseType.FullName, baseFullName, true) == 0)
                {
                    foundType = type;
                    break;
                }
            }
            return foundType;
        }
    }

  

   
  
  
    [DataServiceKey("PartitionKey", "RowKey")]
    public class LogLevel : TableEntity
    {
        // By default, when creating a new entity, the PartitionKey is set to the current year, and the RowKey is a GUID. Insert the ticks in the beginning of RowKey because the result returned by a query is ordered by PartitionKey and then RowKey.
        public LogLevel()
        {
            this.PartitionKey = DateTime.UtcNow.ToString("yyyy");
            this.RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());
        }

        public LogLevel(string partitionKey, string rowKey)
        {
            this.PartitionKey = partitionKey;
            this.RowKey = rowKey;
        }
        public string Level { get; set; }
        public string Description { get; set; }
    }
   
    public class TableUpdateCommand : TableStorageCommand
    {
        protected override void CreateTask(Action action)
        {
            base.CreateTask(() => { RunCommand(); });
        }
        public OutputChannel RunCommand()
        {
            try
            {
                var tableAPI = GetTableAccount();
                var tables = tableAPI.ListTables();
                if (tables.Any())
                {
                    Results.WriteLine("Connected {0} Showing tables", tableAPI.BaseUri);
                    Results.WriteLine("\t{0:-30}\t{1}", "Name", "Uri");
                }
                foreach (var table in tables)
                {
                    Results.WriteLine("\t{0:-30}\t{1}", table.Name, table.Uri);
                }
            }
            catch (Exception azureEx)
            {
                Results.WriteLine("Error updating table {0} ", azureEx.Message);
            }
            return Results;
        }

    }
    public class TableOptionsCommand : TableStorageCommand
    {
        protected override void CreateTask(Action action)
        {
            base.CreateTask(() => { RunCommand(); });
        }
        public OutputChannel RunCommand()
        {
            try
            {
                string otype = objectName;
                Assembly hosted = Assembly.GetExecutingAssembly();
                string baseFullName = (typeof(TableServiceEntity)).FullName;
                Type[] tableTypes = (from a in hosted.GetTypes()
                                     where a.BaseType != null
                                     select a).ToArray();
                Results.WriteLine("================================================================================");

                foreach (var type in tableTypes)
                {
                    if (string.Compare(type.BaseType.FullName, baseFullName, true) == 0)
                    {
                        Results.WriteKeyPair(type.Name, type.Namespace, 10);
                    }
                }
            }
            catch (Exception azureEx)
            {
                Results.WriteLine("Error getting options on table : {0} ", azureEx.Message);
            }
            return Results;
        }

    }
}
